package com.winsdom.entity.customer;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户企业创建实体类
 * 用于接收创建企业客户的请求参数
 */
@Data
@Schema(name = "CustomerEnterpriseCreateRequest", description = "企业客户创建请求参数")
public class CustomerEnterpriseCreateRequest implements Serializable {
    private static final long serialVersionUID = 1L;

    @Schema(description = "客户ID（系统自动生成，创建时无需提供）", hidden = true)
    private Long customerId;

    // === 客户主表字段 ===
    @Schema(description = "客户类型（1-企业客户）", example = "1", required = true)
    @NotNull(message = "客户类型不能为空")
    @Min(value = 1, message = "客户类型无效")
    @Max(value = 1, message = "客户类型无效")
    private Integer customerType;

    @Schema(description = "客户名称", example = "最新的企业8",required = true)
    @NotBlank(message = "客户名称不能为空")
    @Size(min = 2, max = 50, message = "客户名称长度应为2至50个字符")
    private String customerName;

    @Schema(description = "手机号（该客户的管理员，用该手机号登录业主端后可进行同事管理）", example = "19942888888", required = true)
    @NotBlank(message = "手机号不能为空")
    @Pattern(regexp = "^1\\d{10}$", message = "手机号格式不正确，应为11位数字且以1开头")
    private String mobilePhone;

    @Schema(description = "电子邮箱", example = "updated888@example.com")
    @Email(message = "邮箱格式不正确")
    @Size(max = 100, message = "邮箱长度不能超过100个字符")
    private String email;

    @Schema(description = "法律程序状态（1-正常，2-诉讼中，3-破产中）", example = "1")
    @Min(value = 1, message = "法律程序状态无效")
    @Max(value = 3, message = "法律程序状态无效")
    private Integer legalProcedure;

    @Schema(description = "客户状态（0-禁用，1-启用）", example = "1")
    @Min(value = 0, message = "客户状态无效")
    @Max(value = 1, message = "客户状态无效")
    private Integer status;

    @Schema(description = "更新用户ID（当前操作的用户ID）", example = "1")
    private Long updateUserId;

    // === 企业扩展表字段 ===
    @Schema(description = "注册号（统一社会信用码）", example = "96678900ILHRW8888", required = true)
    @NotBlank(message = "统一社会信用码不能为空")
    @Pattern(regexp = "^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$", message = "统一社会信用码格式不正确，应为15、18或20位数字或英文")
    private String registrationNumber;

    @Schema(description = "企业性质（单选，读取字典表企业性质的数据）", example = "合资", required = true)
    @Size(max = 50, message = "企业性质长度不能超过50个字符")
    private String enterpriseNature;

    @Schema(description = "经营性质（单选，读取字典表经营性质的数据）", example = "农业",required = true)
    @Size(max = 50, message = "经营性质长度不能超过50个字符")
    private String businessNature;

    @Schema(description = "纳税身份（1-一般纳税人，2-小规模纳税人）", example = "1", required = true)
    @Min(value = 1, message = "纳税身份无效")
    @Max(value = 2, message = "纳税身份无效")
    private Integer taxStatus;

    @Schema(description = "是否重点招商引资客户（0-否，1-是）", example = "0")
    @Min(value = 0, message = "是否重点招商引资客户值无效")
    @Max(value = 1, message = "是否重点招商引资客户值无效")
    private Integer keyInvestment;

    @Schema(description = "开户行", example = "Bank of China",required = true)
    @Size(min = 4, max = 30, message = "开户行名称长度应为4至30个字符")
    private String bankName;

    @Schema(description = "银行账号", example = "88888888888888", required = true)
    @Pattern(regexp = "^\\d{12,25}$", message = "银行账号格式不正确，应为12至25位数字")
    private String bankAccount;

    @Schema(description = "注册资金（万元）", example = "1333332.00",required = true)
    @DecimalMin(value = "0.01", message = "注册资金不能小于0.01万元")
    @Digits(integer = 10, fraction = 2, message = "注册资金格式不正确，只能输入数字和小数点，保留两位小数")
    private BigDecimal registeredCapital;

    @Schema(description = "员工数量", example = "110",required = true)
    @Pattern(regexp = "^\\d+$", message = "员工数量格式不正确，只能是数字")
    private Integer employeeCount;

    // === 地址信息 ===
    @Schema(description = "地址信息列表（至少包含一个地址，类型为1-注册地址，2-办公地址，最多5个地址）",required = true)
    @Size(max = 5, message = "地址数量不能超过5个")
    private List<@Valid AddressInfo> addresses;

    // === 联系人信息 ===
    @Schema(description = "联系人信息列表（主要联系人应放在首位，最多添加5个联系人）", required = true)
    @Size(max = 5, message = "联系人数量不能超过5个")
    private List<@Valid ContactInfo> contactInfos;

    // === 证书信息 ===
    @Schema(description = "营业执照图片URL", example = "/images/business_license8.jpg", required = true)
    @Size(max = 255, message = "图片URL长度不能超过255个字符")
    private String businessLicenseImage;

    // === 通知配置信息 ===
    @Schema(description = "付款通知是否启用（0-禁用，1-启用）", example = "1")
    @Min(value = 0, message = "付款通知启用状态无效")
    @Max(value = 1, message = "付款通知启用状态无效")
    private Integer paymentNotifyEnabled;

    @Schema(description = "付款通知类型（1-短信，2-邮件，3-短信+邮件）", example = "2")
    @Min(value = 1, message = "付款通知类型无效")
    @Max(value = 3, message = "付款通知类型无效")
    private Integer paymentNotifyType;

    @Schema(description = "收款通知是否启用（0-禁用，1-启用）", example = "1")
    @Min(value = 0, message = "收款通知启用状态无效")
    @Max(value = 1, message = "收款通知启用状态无效")
    private Integer collectionNotifyEnabled;

    @Schema(description = "收款通知类型（1-短信，2-邮件，3-短信+邮件）", example = "2")
    @Min(value = 1, message = "收款通知类型无效")
    @Max(value = 3, message = "收款通知类型无效")
    private Integer collectionNotifyType;

    @Schema(description = "收款后通知天数（收款后多少天发送通知）", example = "10")
    @Min(value = 0, message = "收款后通知天数不能小于0")
    private Integer collectionAfterDays;

    @Schema(description = "首次通知间隔（小时）", example = "1")
    @Min(value = 1, message = "首次通知间隔不能小于1小时")
    private Integer firstNoticeInterval;

    @Schema(description = "最大通知次数（逾期后最多通知次数）", example = "5")
    @Min(value = 1, message = "最大通知次数不能小于1")
    private Integer maxNoticeTimes;

    // === 票据配置信息 ===
    @Schema(description = "开票类型（可选值：增值税专用发票、增值税普通发票、电子发票、其他）", example = "纸质收据")
    @Size(max = 50, message = "开票类型长度不能超过50个字符")
    private String billingType;

    @Schema(description = "开票方式（可选值：按订单、按日汇总、按周汇总、按月汇总）", example = "先款后票")
    @Size(max = 50, message = "开票方式长度不能超过50个字符")
    private String billingMethod;

    @Schema(description = "自定义备注是否启用（0-禁用，1-启用）", example = "0")
    @Min(value = 0, message = "自定义备注启用状态无效")
    @Max(value = 1, message = "自定义备注启用状态无效")
    private Integer customRemarkEnabled;

    @Schema(description = "自动开票是否启用（0-禁用，1-启用）", example = "0")
    @Min(value = 0, message = "自动开票启用状态无效")
    @Max(value = 1, message = "自动开票启用状态无效")
    private Integer autoBillingEnabled;

    @Schema(description = "开票规则（自定义开票规则描述）", example = "合并开票")
    @Size(max = 200, message = "开票规则长度不能超过200个字符")
    private String billingRule;

    // === 地址内部类 ===
    @Data
    @Schema(name = "CustomerEnterpriseCreateRequest.AddressInfo", description = "企业地址信息")
    public static class AddressInfo {

        @Schema(description = "客户ID（系统自动生成，创建时无需提供）", hidden = true)
        private Long customerId;

        @Schema(description = "地址类型（1-注册地址，2-办公地址，3-发货地址，4-收货地址，5-其他地址）", example = "1",required = true)
        @Min(value = 1, message = "地址类型无效")
        @Max(value = 5, message = "地址类型无效")
        private Integer addressType;

        @Schema(description = "地址区域", example = "[\"浙江省\", \"杭州市\", \"西湖区\"]",required = true)
        @Size(max = 100, message = "地址区域长度不能超过100个字符")
        private String area;

        @Schema(description = "地址区域码", example = "[\"33\", \"3301\", \"330106\"]", required = true)
        @Size(max = 20, message = "地址区域码长度不能超过20个字符")
        private String areaCode;

        @Schema(description = "详细地址", example = "Chongqing City, Detailed Address 1",required = true)
        @Size(max = 200, message = "详细地址长度不能超过200个字符")
        private String detailedAddress;
    }

    // === 联系人内部类 ===
    @Data
    @Schema(name = "CustomerEnterpriseCreateRequest.ContactInfo", description = "企业联系人信息")
    public static class ContactInfo {

        @Schema(description = "客户ID（系统自动生成，创建时无需提供）", hidden = true)
        private Long customerId;

        @Schema(description = "联系人姓名", example = "刘东",required = true)
        @Size(min = 2, max = 50, message = "联系人姓名长度应为2至50个字符")
        private String contactName;

        @Schema(description = "联系人手机", example = "13567645888", required = true)
        @Pattern(regexp = "^\\d{4,11}$", message = "联系人手机号格式不正确，应为4至11位数字")
        private String contactMobile;

        @Schema(description = "联系人邮箱", example = "liudong1@example.com")
        @Email(message = "联系人邮箱格式不正确")
        @Size(max = 100, message = "联系人邮箱长度不能超过100个字符")
        private String contactEmail;

        @Schema(description = "接收属性列表（可选值：报价单、订单通知、发货通知、发票信息、对账单、付款提醒）", example = "[\"金星大厦\"]",required = true)
        private List<String> receiveProperties;
    }
}